/*
 * 版权所有(c)  1952110078@qq.com  2022 - 2022
 */

package com.ldc.commonlibs.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @author liudc
 * @since 2022/7/17 01:22
 */
public class JsonUtil {


    public static <T> List<T> jsonToModelByType(String json, Class<T> clazz) {
        if (TextUtils.isEmpty(json)) {
            return null;
        }
        try {
            Type type = new TypeToken<T>() {
            }.getType();
            return new Gson().fromJson(json, type);
        } catch (Exception e) {
            return null;
        }
    }

    public static <T> T jsonToModel(String source, Class<T> clazz) {
        if (TextUtils.isEmpty(source)) {
            return null;
        }
        try {
            return new Gson().fromJson(source, clazz);
        } catch (Exception e) {
            return null;
        }
    }
}
